package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.RoleService;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping(value = "/findAll")
    public Result findAll() {
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true,"",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }

    @RequestMapping(value = "/add")
    public Result add(@RequestBody Role role, @RequestParam(value = "userIds") Integer[] userIds,@RequestParam(value = "menuIds") Integer[] menuIds) {
        try {
            roleService.add(role,userIds,menuIds);
            return new Result(true,"新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
    }
    //新增user
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean.getQueryString(),
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());
        return pageResult;
    }

    //通过userid查询user
    @RequestMapping(value = "/findByRoleId")
    public Result findByRoleId(Integer id) {
        Role role = roleService.findById(id);
        if(role!=null){
            return new Result(true,"查询角色成功",role);
        }
        return new Result(false,"查询角色失败");
    }


    @RequestMapping(value = "/findUserIdsByRoleId")
    public Result findUserIdsByRoleId(Integer id) {
        try {
            List<Integer> userIds=roleService.findUserIdsByRoleId(id);
            return new Result(true,"获取用户信息成功",userIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户信息失败");
        }
    }
    @RequestMapping(value = "/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> menuIds=roleService.findMenuIdsByRoleId(id);
            return new Result(true,"获取菜单信息成功",menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取菜单信息失败");
        }
    }

    @RequestMapping(value = "/editRole")
    public Result editRole(@RequestBody Role role, @RequestParam(value = "userIds") Integer [] userIds,@RequestParam(value = "menuIds") Integer [] menuIds) {
        try {
            roleService.editRole(role,userIds,menuIds);
            return new Result(true,"编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
    }
    //编辑
    @RequestMapping(value = "/deleteRole")
    public Result deleteRole(Integer id) {
        try {
            roleService.deleteRole(id);
            return new Result(true,"删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
    }
}
